package com.easy.stock.spider.html;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.easy.stock.dao.StockBreakingNewsDao;
import com.easy.stock.dao.StockInfoDao;
import com.easy.stock.domain.StockArticle;
import com.easy.stock.domain.StockInfo;
import com.easy.stock.spider.HttpResponseHandler;
import com.easy.stock.spider.SpiderTask;
import com.easy.stock.spider.SpiderTask.STATUS;
import com.easy.stock.util.Constants;
import com.easy.stock.util.JsoupUtil;

public class SABreakingNewsSpider extends BaseHtmlSpider {

	@Autowired
	private StockInfoDao stockInfoDao;
	
	@Autowired
	private StockBreakingNewsDao stockBreakingNewsDao;
	
	private static String SITE_URL="http://seekingalpha.com";
	private String baseUrl;
	
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	
	@Override
	protected List<SpiderTask> prepareSeeds() {
		
		List<SpiderTask> tasks = new ArrayList<SpiderTask>();
		List<StockInfo> stocks = null;
		int limit=1000;
		int offset=0;
		while(!(stocks=stockInfoDao.getStockInfoList(offset, limit)).isEmpty()) {
			for(StockInfo stock : stocks) {
				SpiderTask task = new SpiderTask(baseUrl+stock.getId()+"/news", new DetailHandler(this, stock.getId()), STATUS.READY);
				tasks.add(task);
			}
			
			offset+=limit;
		}
		return tasks;
		
//		// for test
//		List<SpiderTask> tasks = new ArrayList<SpiderTask>();
//		SpiderTask task = new SpiderTask(baseUrl+"INSY/news", new DetailHandler(this, "INSY"), STATUS.READY);
//		tasks.add(task);
//		return tasks;
	}
	
	public static class DetailHandler extends HttpResponseHandler {

		private String stockId;
		
		public DetailHandler(BaseHtmlSpider spider, String stockId) {
			super.spider = spider;
			this.stockId = stockId;
		}
		
		private String getDayString(int interval) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_YEAR, interval);
			SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM. d", Locale.US);
			return sdf.format(cal.getTime());
		}
		
		@Override
		public List<SpiderTask> doOnCompleted(HttpResponse response)
				throws IOException {
			
			SABreakingNewsSpider mySpider = (SABreakingNewsSpider)spider;
			String content = response.getContent().toString(Charset.forName("UTF-8"));;
			Document doc = Jsoup.parse(content);
			
			List<SpiderTask> tasks = new ArrayList<SpiderTask>();
			Elements elems = doc.select("div.quote_list_area ul li div.mc_list_texting");
			int elemNum = elems.size();
			SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM. d, h:mm aa yyyy", Locale.US);
			String year = new SimpleDateFormat("yyyy").format(new Date());
			int newsNum=0;
			for(int i=0; i<elemNum; i++) {
				Element elem = elems.get(i);
				
				StockArticle article = new StockArticle();
				String title = JsoupUtil.getElemHtml(elem, "div.mc_bullets_title a", false);
				if(title.length()==0) {
					// 无标题的不抓取
					continue;
				}
				article.setTitle(title);
				article.setUrl(SITE_URL+JsoupUtil.getElemProp(elem,  "div.mc_bullets_title a", "href", true));
				String cnt = JsoupUtil.getElemHtml(elem, "span.light_text", false);
				if(cnt.length()==0) {
					// 无内容部抓取
					continue;
				}
				
				article.setContent(cnt);
				String dateStr = JsoupUtil.getElemHtml(elem, "span.mc_new span.date", true);
				
				if(dateStr.indexOf("2013")>=0)
					continue;
				dateStr = dateStr.replace("Today", getDayString(0));
				dateStr = dateStr.replace("Yesterday", getDayString(-1));
				dateStr = dateStr+" "+year;
				try {
					article.setDate(sdf.parse(dateStr));
				} catch (ParseException e) {
					throw new RuntimeException("fail to parse breaking news date, str="+dateStr);
				}
				article.setSrc(Constants.SEEKING_ALPHA);
				article.setStock_id(stockId);
				mySpider.stockBreakingNewsDao.addArticle(article);
				newsNum++;
			}
			
			LOG.info("finish crawl stock breaking news page, stockId="+stockId+", news="+newsNum);
			return tasks;
		}
		
		@Override
		public void doOnThrowable(Throwable t) {
			// do nothing
		}
		
	}
}
